package com.goods.business.service.imp;




import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.CategoryMapper;
import com.goods.business.service.CategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.system.Department;
import com.goods.common.utils.MenuTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.DepartmentVO;
import com.goods.common.vo.system.MenuNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService  {
    @Autowired
    private CategoryMapper categoryMapper;



    //去除集合中为null的元素
    public List<ProductCategoryTreeNodeVO> removeNull(List<ProductCategoryTreeNodeVO> oldList){
        oldList.removeAll(Collections.singleton(null));
        return oldList;
    }

    //加载分类数据的分类树
    @Override
    public List<ProductCategoryTreeNodeVO> getCategoryListTree() {
        List<ProductCategory> productCategoryList = categoryMapper.selectAll();

        //获得productCategoryVOList的集合
        List<ProductCategoryVO> productCategoryVOList = productCategoryList.stream().map(productCategory -> {
            ProductCategoryVO p = new ProductCategoryVO();
            BeanUtils.copyProperties(productCategory, p);
            return p;
        }).collect(Collectors.toList());

        List<ProductCategoryTreeNodeVO> allTree = productCategoryVOList.stream().map(productCategoryVO -> {
            ProductCategoryTreeNodeVO tree = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategoryVO, tree);
            return tree;
        }).collect(Collectors.toList());

        //获得父亲树集合
        List<ProductCategoryTreeNodeVO> tree1 = productCategoryVOList.stream().map(productCategoryVO -> {
            ProductCategoryTreeNodeVO tree = new ProductCategoryTreeNodeVO();
            if (productCategoryVO.getPid() == 0) {
                BeanUtils.copyProperties(productCategoryVO, tree);
                tree.setLev(1);
                return tree;
            }
            return null;
        }).collect(Collectors.toList());
        this.removeNull(tree1);

        //儿子
        tree1.stream().forEach(productCategoryTreeNodeVO -> {
            List<ProductCategoryTreeNodeVO> tree2 = allTree.stream().map(all -> {
                ProductCategoryTreeNodeVO tree = new ProductCategoryTreeNodeVO();
                if (all.getPid() == productCategoryTreeNodeVO.getId()) {
                    BeanUtils.copyProperties(all, tree);
                    //孙子
                    List<ProductCategoryTreeNodeVO> tree3 = allTree.stream().map(tree33 -> {
                        ProductCategoryTreeNodeVO tree333 = new ProductCategoryTreeNodeVO();
                        if (tree33.getPid() == tree.getId()){
                            BeanUtils.copyProperties(tree33, tree333);
                            tree333.setLev(3);
                            return tree333;
                        }
                        return null;
                    }).collect(Collectors.toList());
                    this.removeNull(tree3);
                    tree.setChildren(tree3);
                    tree.setLev(2);
                    //返回符合情况的二级分类
                    return tree;
                }
                return null;
            }).collect(Collectors.toList());
            this.removeNull(tree2);
            productCategoryTreeNodeVO.setChildren(tree2);
        });
        return tree1;
    }

    //添加分类
    @Override
    public void add(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        Date date = new Date();
        productCategory.setCreateTime(date);
        productCategory.setModifiedTime(date);
        //获取所选二级分类的id,设置pid
        productCategory.setPid(productCategoryVO.getPid());

        categoryMapper.insert(productCategory);
    }



    //加载父级分类数据
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("pid",0);
        List<ProductCategory> list1 = categoryMapper.selectByExample(example);
        //一级分类的tree
        List<ProductCategoryTreeNodeVO> tree1 = list1.stream().map(productCategory -> {
            ProductCategoryTreeNodeVO treeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory, treeVO);
            return treeVO;
        }).collect(Collectors.toList());

        //二级分类的tree
        Example example1 = new Example(ProductCategory.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.orEqualTo("pid",1);
        List<ProductCategory> list2 = categoryMapper.selectByExample(example1);
        List<ProductCategoryTreeNodeVO> tree2 = list2.stream().map(productCategory -> {
            ProductCategoryTreeNodeVO treeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory, treeVO);
            return treeVO;
        }).collect(Collectors.toList());

        List<ProductCategoryTreeNodeVO> result = tree1.stream().map(t1 -> {
            List<ProductCategoryTreeNodeVO> treeIn = tree2.stream().map(t2 -> {
                if (t2.getPid() == t1.getId()) {
                    return t2;
                }
                return null;
            }).collect(Collectors.toList());
            this.removeNull(treeIn);
            t1.setChildren(treeIn);
            return t1;
        }).collect(Collectors.toList());
        return result;
    }

    //打开修改页面弹出框,并且回显数据
    @Override
    public ProductCategoryVO getEdit(Long id) {
        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("id",id);
        ProductCategory productCategory = categoryMapper.selectOneByExample(example);
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory,productCategoryVO);
        return productCategoryVO;
    }

    //修改信息
    @Override
    public void edit(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        categoryMapper.updateByPrimaryKey(productCategory);
    }

    //删除分类
    @Override
    public void delete(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        categoryMapper.deleteByPrimaryKey(productCategory);
    }


}
